/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.vfi.be.TipoTransaccion;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class TipoTransaccionDelegate {

    private TipoTransaccionFacadeLocal local;

    public TipoTransaccionDelegate() {
        local = lookupTipoTransaccionFacade();
    }

    public void crear(TipoTransaccion tipoTransaccion) throws Exception {
        try {
            local.create(tipoTransaccion);
        } catch (Exception e) {
            local = lookupTipoTransaccionFacade();
            throw new Exception("Crear TipoTransaccion. " + e.getMessage(), e);
        }
    }

    public void editar(TipoTransaccion tipoTransaccion) throws Exception {
        try {
            local.edit(tipoTransaccion);
        } catch (Exception e) {
            local = lookupTipoTransaccionFacade();
            throw new Exception("Editar TipoTransaccion. " + e.getMessage(), e);
        }
    }

    public void eliminar(TipoTransaccion tipoTransaccion) throws Exception {
        try {
            local.remove(tipoTransaccion);
        } catch (Exception e) {
            local = lookupTipoTransaccionFacade();
            throw new Exception("Eliminar TipoTransaccion. " + e.getMessage(), e);
        }
    }

    public TipoTransaccion buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupTipoTransaccionFacade();
            throw new Exception("Buscar TipoTransaccion. " + e.getMessage(), e);
        }
    }

    public List<TipoTransaccion> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupTipoTransaccionFacade();
            throw new Exception("Listar TipoTransaccion. " + e.getMessage(), e);
        }
    }

    private TipoTransaccionFacadeLocal lookupTipoTransaccionFacade() {
        try {
            Context c = new InitialContext();
            return (TipoTransaccionFacadeLocal) c.lookup("java:comp/env/TipoTransaccionFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
